﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EmbedlyNet
{
    /// <summary>
    /// Displays media using the emebdly service. 
    /// 
    /// Requires 
    /// - jquery 1.4 or higher
    /// - jquery emebdly (http://github.com/embedly/embedly-jquery/blob/master/jquery.embedly.js)
    /// 
    /// Required property set the Property of the embedly url on the page..
    /// 
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:EmbedlyControl EmbedlyUrl=\"http://www.youtube.com/watch?v=Mvfn8QlXPG8\" runat=server></{0}:EmbedlyControl>")]
    public class EmbedlyControl : WebControl
    {

        private string embedlyUrl;
        public EmbedlyControl()
        {
            embedlyUrl = "";
        }


        public string EmbedlyUrl
        {
            get { return embedlyUrl; }
            set { embedlyUrl = value; }
        }

        protected override void OnInit(EventArgs e)
        {

            // checks if the url passed is one of embeldys supported services.
            if (EmbedlyHelper.ValidateUrl(embedlyUrl))
            {

                // the following is a test to make sure that no there is actual content associated with the url
                // this gives the user a proper error message.. 
                // depending on requirements, some sort of caching could be implemented here as well. 
                string json = EmbedlyHelper.GetJson(embedlyUrl);

                if (json != null) 
                {
                    Literal lit = new Literal();
                    string html = "<a class='embedlyNet' href='" + embedlyUrl + "'></a>";
                    lit.Text = html;
                    this.Controls.Add(lit);

                    string onStart = "";
                    onStart += "$('.embedlyNet').embedly(";
                    onStart += "{";
                    // set further options as needed here.
                    onStart += "    maxWidth: 300,";
                    //onStart += "    maxHeight: maxHeight,";
                    //onStart += "    urlRe: urlRe,";
                    onStart += "    method: 'replace',";
                    onStart += "    wrapElement: 'div',";
                    onStart += "    className: 'embedlyNet',";
                    onStart += "    addImageStyles: 'true'";
                    onStart += "});";
                    this.AddJqueryDocumentReadyScript(onStart, "embedlyNet");
                    return;

                }
            }


            Literal lit2 = new Literal();
            string html2 = "<div>No or invalid Embedly url given.. : Url "+embedlyUrl+"</div>";
            lit2.Text = html2;
            this.Controls.Add(lit2);

        }
        private void AddJqueryDocumentReadyScript(string scriptToInsert, string scriptId)
        {
            string script = "";
            script = @"
                <script language = 'javascript'>                         
                        $(document).ready(function(){";
            script += scriptToInsert;
            script += @"});</script>";
            this.Page.ClientScript.RegisterClientScriptBlock(typeof(string),"embedlyNet", script);
        }
    }
}
